##### Loading Packages ####
packages <- c("dplyr",   "conflicted", "stringi",
              "sampleSelection", "lmtest", "tidyr", "marginaleffects",
              "ggpubr", "texreg",  "xtable", "sf" , "sandwich", "purrr",
              "MatchIt", "cobalt")
sapply(packages, function(x){library(x, character.only = TRUE)})
conflict_prefer("select", "dplyr")
conflict_prefer("filter", "dplyr")

#### Defining functions ####
vcovCL <- function(fm, cluster) {
  
  M <- length(unique(cluster))
  N <- length(cluster)
  dfc <- M/(M-1)
  
  u  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum))
  dfc * sandwich(fm, meat = crossprod(u)/N)
}


makeSelectionModelTable <- function(modelList, 
                                    coefMapping, 
                                    caption, 
                                    selectionOutcome, 
                                    outcomeOutcome, 
                                    filename, 
                                    size, 
                                    label){
  selectionTable =  huxtablereg(modelList, 
                                override.se = lapply(modelList, 
                                                     function(x){
                                                       tmp <- coeftest(x, .vcov = vcovCL(x, petitionsByLosingCandidate$constituency))
                                                       return(tmp[,2])
                                                     }), 
                                override.pvalues = lapply(modelList, 
                                                          function(x){
                                                            tmp <- coeftest(x, .vcov = vcovCL(x, petitionsByLosingCandidate$constituency))
                                                            return(tmp[,4])
                                                          }), 
                                custom.coef.map = coefMapping)
  selectionTable = as_tibble(selectionTable)
  selectionStart = as.numeric(rownames(selectionTable))[selectionTable$V1 == "S: Intercept"]
  outcomeStart = as.numeric(rownames(selectionTable))[selectionTable$V1 == "O: Intercept"]
  selectionEnd = outcomeStart -1
  tableEnd = nrow(selectionTable)
  equationRows = as.data.frame(matrix(NA, nrow = 2, ncol = ncol(selectionTable)))
  colnames(equationRows) = colnames(selectionTable)
  equationRows$V1 = c("Selection equation:",  "Outcome equation:")
  selectionTable = rbind(selectionTable[1,], 
                         equationRows[1,],
                         t(data.frame(selectionOutcome)),
                         selectionTable[selectionStart:selectionEnd,], 
                         equationRows[2,],
                         t(data.frame(outcomeOutcome)),
                         selectionTable[outcomeStart:tableEnd,])
  selectionTable$V1 = stri_replace_all_fixed(selectionTable$V1, "S: ", "")
  selectionTable$V1 = stri_replace_all_fixed(selectionTable$V1, "O: ", "")
  
  selectionTable = filter(selectionTable, V1 != "R^2", V1 != "Adj. R^2", V1 != "AIC",  V1 != "BIC")
  
  #Haakon added 24-04-2024
  comment          <- list()
  comment$pos      <- list()
  comment$pos[[1]] <- c(nrow(selectionTable))
  
  comment$command  <- paste0("\\hline \\multicolumn{", ncol(selectionTable), "}{l}{Note: * p < 0.05, ** p < 0.01, *** p < 0.001. Standard errors clustered on constituency in parentheses.} \\\\")
  
  
  print(xtable(selectionTable, caption = caption, 
               label = label),
        file = filename, 
        type = "latex", 
        caption.placement = "top", 
        hline.after = c(-1, 1, selectionEnd+2, outcomeStart+selectionEnd+4),#, nrow(selectionTable)),
        size = size, 
        add.to.row = comment,
        include.rownames = FALSE, 
        include.colnames = FALSE)
}


##### Loading data  ####
load("Data/ElectoralPetitionsData.RData")


#### Estimating selection models #####



selection1 <- selection(selection = formula(petition ~ 
                                              candidate_from_winning_party + 
                                              respondent_from_preelection_ruling_party +
                                              won_by_pre_election_minister+ 
                                              acled_electoral_violence_dummy+
                                              PopulationDensityLog+
                                              year_character + 
                                              other_petitions +
                                              distance_to_winning_vote_share), 
                        outcome = formula(final_outcome_nullification_logical ~ 
                                            candidate_from_winning_party + 
                                            respondent_from_preelection_ruling_party +
                                            PopulationDensityLog+
                                            won_by_pre_election_minister +
                                            acled_electoral_violence_dummy +
                                            year_character + 
                                            other_petitions +
                                            distance_to_winning_vote_share),  
                        data = petitionsByLosingCandidate, 
                        method = "ml")
coeftest(selection1, 
         .vcov = vcovCL(selection1, petitionsByLosingCandidate$constituency))


table(petitionsByLosingCandidate$petition_and_litigation== 1  )

selection1b <- selection(selection = formula(petition_and_litigation ~ 
                                               candidate_from_winning_party + 
                                               respondent_from_preelection_ruling_party +
                                               won_by_pre_election_minister+ 
                                               petition_spatial_lag+
                                               acled_electoral_violence_dummy+
                                               PopulationDensityLog +
                                               year_character + 
                                               other_petitions +
                                               distance_to_winning_vote_share), 
                         outcome = formula(final_outcome_nullification_logical ~ 
                                             candidate_from_winning_party + 
                                             respondent_from_preelection_ruling_party +
                                             won_by_pre_election_minister +
                                             PopulationDensityLog +
                                             acled_electoral_violence_dummy+
                                             year_character + 
                                             other_petitions +
                                             distance_to_winning_vote_share),  
                         data = petitionsByLosingCandidate, 
                         method = "ml")
coeftest(selection1b, .vcov = vcovCL(selection1b, petitionsByLosingCandidate$constituency))

selection1c <- selection(selection = formula(petition ~ 
                                              candidate_from_winning_party + 
                                             won_by_pre_election_minister+ 
                                               
                                               violence_2016_2021+
                                              year_character + 
                                              other_petitions +
                                              distance_to_winning_vote_share), 
                        outcome = formula(final_outcome_nullification_logical ~ 
                                            candidate_from_winning_party + 
                                           won_by_pre_election_minister+ 
                                            violence_2016_2021+
                                            year_character + 
                                             other_petitions + 
                                            distance_to_winning_vote_share),
                        data = filter(petitionsByLosingCandidate, year > 2011),
                        method = "ml")

coeftest(selection1c, .vcov = vcovCL(selection1c , filter(petitionsByLosingCandidate, year > 2011)$constituency))




selection2 <- selection(selection = formula(petition ~ 
                                              candidate_from_winning_party + 
                                              respondent_from_preelection_ruling_party +
                                              won_by_pre_election_minister+ 
                                              acled_electoral_violence_dummy+
                                              PopulationDensityLog +
                                              year_character + 
                                              other_petitions +
                                              distance_to_winning_vote_share), 
                        outcome = formula(nullified_by_high_court_logical~ 
                                            candidate_from_winning_party + 
                                            respondent_from_preelection_ruling_party +
                                            won_by_pre_election_minister+ 
                                            acled_electoral_violence_dummy+
                                            PopulationDensityLog +
                                            year_character + 
                                            other_petitions +
                                            distance_to_winning_vote_share),  
                        data = petitionsByLosingCandidate, 
                        method = "ml")
coeftest(selection2, .vcov = vcovCL(selection2, petitionsByLosingCandidate$constituency))

selection2b <- selection(selection = formula(petition_and_litigation ~ 
                                               candidate_from_winning_party + 
                                               respondent_from_preelection_ruling_party +
                                               won_by_pre_election_minister+ 
                                               acled_electoral_violence_dummy+
                                               PopulationDensityLog +
                                               year_character + 
                                               other_petitions +
                                               distance_to_winning_vote_share), 
                         outcome = formula(nullified_by_high_court_logical ~ 
                                             candidate_from_winning_party + 
                                             respondent_from_preelection_ruling_party +
                                             won_by_pre_election_minister+ 
                                             acled_electoral_violence_dummy+
                                             PopulationDensityLog +
                                             year_character + 
                                             other_petitions +
                                             distance_to_winning_vote_share),  
                         data = petitionsByLosingCandidate, 
                         method = "ml")
coeftest(selection2b, .vcov = vcovCL(selection2b, petitionsByLosingCandidate$constituency))


selection2c <- selection(selection = formula(petition ~ 
                                               candidate_from_winning_party +
                                               respondent_from_preelection_ruling_party+
                                               won_by_pre_election_minister +
                                               violence_2016_2021+
                                               year_character + 
                                               other_petitions +
                                               distance_to_winning_vote_share), 
                         outcome = formula(nullified_by_high_court_logical ~ 
                                             candidate_from_winning_party +
                                             respondent_from_preelection_ruling_party +
                                             won_by_pre_election_minister +
                                             violence_2016_2021+
                                             year_character + 
                                             other_petitions +
                                             distance_to_winning_vote_share),  
                         data = filter(petitionsByLosingCandidate, year > 2011),
                         method = "ml")
coeftest(selection2c, .vcov = vcovCL(selection2c, petitionsByLosingCandidate$constituency))




selection3  <- selection(selection = formula(appeal_dummy ~ 
                                               high_court_loser_from_winning_party + high_court_outcome+
                                               respondent_from_preelection_ruling_party +
                                               won_by_pre_election_minister +
                                                distance_to_winning_vote_share +
                                                other_petitions 
                                             ),
                         outcome = formula(appeal_success ~
                                             high_court_loser_from_winning_party +
                                           high_court_outcome+
                                            respondent_from_preelection_ruling_party +
                                            won_by_pre_election_minister +
                                             distance_to_winning_vote_share +
                                             other_petitions  ),
                         data = filter(petitionsByLosingCandidate, 
                                       high_court_outcome == "Petition dismissed" |
                                         high_court_outcome == "Election nullified"),
                         method = "ml")

coeftest(selection3 , .vcov = vcovCL(selection3, filter(petitionsByLosingCandidate, 
                                                       high_court_outcome == "Petition dismissed" |
                                                         high_court_outcome == "Election nullified")$constituency))



firstStagePrediction1 <- plot_predictions(selection1, condition = c( "candidate_from_winning_party", "year_character"), 
                                           vcov = vcov(selection1), draw = FALSE) %>%
  ggplot(aes(x = year_character,
             y = estimate,
             shape = candidate_from_winning_party ,
             color = candidate_from_winning_party ,
             ymin = conf.low,
             ymax = conf.high))+
  geom_errorbar(width = 0, position = position_dodge(width=0.1), linewidth = 1)+
  geom_point(position = position_dodge(width=0.1), size = 3)+
  scale_color_grey(end = 0.5, labels = c("Candidate from losing party", "Candidate from winning party")) +
  scale_shape(labels      = c("Candidate from losing party", "Candidate from winning party")) +
  theme_classic() +
  ylab("Predicted probability of filing\na petition at the High Court level")+
  xlab("")+
  ggtitle("Model 1")+
  ylim(-.1,1)+
  guides(color = guide_legend(reverse = TRUE),
         shape = guide_legend(reverse = TRUE)) +
  theme(legend.title = element_blank(),
        text = element_text(size = 20),
        legend.position = "bottom")
firstStagePrediction1
ggsave(firstStagePrediction1, filename = "FiguresAndTables/selection1_firststage_prediction.jpg", width = 10, height = 10)









makeSelectionModelTable(modelList = list(selection1,selection1b,selection1c,  
                                         selection2, selection2b,selection2c, 
                                         selection3), 
                        coefMapping = list(  "S: (Intercept)" = "S: Intercept",
                                             "S: candidate_from_winning_party" = "S: Candidate from winning party",
                                             "S: high_court_loser_from_winning_party"  = "S: High Court loser from winning party", 
                                             "S: respondent_from_preelection_ruling_party" = "S: Constituency won by pre-election ruling party", 
                                             "S: won_by_pre_election_minister"             = "S: Constituency won by pre-election minister",
                                             "S: other_petitions" =  "S: Other petitions in same constituency",
                                             "S: distance_to_winning_vote_share" = "S: Distance to winning vote share", 
                                             "S: year_character2016" = "S: 2016 Election", 
                                             "S: year_character2021" = "S: 2021 Election", 
                                             "S: appointing_party_high_court_judgepf" = "S: High Court judge appointed by PF", 
                                             "S: high_court_outcomePetition dismissed" = "S: Petition Dismissed in High Court", 
                                             "S: acled_electoral_violence_dummy"       = "S: Electoral violence (ACLED)",
                                             "S: violence_2016_2021"       = "S: Electoral violence (Election monitors)",
                                             "S: PopulationDensityLog"                 = "S: Log population density",
                                             
                                             "O: (Intercept)" = "O: Intercept", 
                                             "O: candidate_from_winning_party" = "O: Candidate from winning party",
                                             "O: high_court_loser_from_winning_party"  = "O: High Court loser from winning party", 
                                             "O: respondent_from_preelection_ruling_party" = "O: Constituency won by pre-election ruling party", 
                                             "O: won_by_pre_election_minister"             = "O: Constituency won by pre-election minister",
                                             "O: other_petitions" =  "O: Other petitions in same constituency",
                                             "O: distance_to_winning_vote_share" = "O: Distance to winning vote share", 
                                             "O: year_character2016" = "O: 2016 Election", 
                                             "O: year_character2021" = "O: 2021 Election", 
                                             "O: appointing_party_high_court_judgepf" = "O: High Court judge appointed by PF", 
                                             "O: high_court_outcomePetition dismissed" = "O: Petition Dismissed in High Court", 
                                             "O: acled_electoral_violence_dummy"       = "O: Electoral violence (ACLED)",
                                             "O: violence_2016_2021"       = "O: Electoral violence (Election monitors)",
                                             
                                             
                                             
                                             "O: PopulationDensityLog"                 = "O: Log population density",
                                             
                                             "rho" = "O: rho"), 
                        selectionOutcome = c("", "Petition", "Litigation",
                                             "Petition",
                                             "Petition", "Litigation",
                                             "Petition", 
                                             "Appeal"),
                        outcomeOutcome = c("", rep("Nullified", times = 3), 
                                           rep("Nullfied by High Court", times = 3), 
                                           "Appeal success"), 
                        caption = "Two-stage selection models",
                        filename = "FiguresAndTables/selectionModels.tex", 
                        size = "tiny", 
                        label = "tab:selectionModels")

probit1 <- glm(nullified_by_high_court_logical ~ appointing_party_high_court_judge + candidate_from_winning_party + 
                 respondent_from_preelection_ruling_party + won_by_pre_election_minister + 
                 PopulationDensityLog + acled_electoral_violence_dummy + year_character + 
                 other_petitions + distance_to_winning_vote_share ,  
               data = filter(petitionsByLosingCandidate, petition_and_litigation == 1, year >2011),
               family = binomial(link = "probit"))



plot_predictions(model = probit1, by = "appointing_party_high_court_judge", draw = FALSE)


plot_predictions(model = probit1, by = "appointing_party_high_court_judge")+
  theme_classic()+
  ylim(0, 0.7) +
  labs(title = "Model 8", 
       y = "Predicted probability of High Court nullification", 
       x = "Appointing party of High Court Judge")+
  scale_x_discrete(labels = c("MMD", "PF"))+
  theme(text = element_text(size = 20))
ggsave(filename = "FiguresAndTables/Model6predictions.jpg", height = 7, width = 7)


probit2 <- glm(nullified_by_high_court_logical ~ appointing_party_high_court_judge * I(winning_party == "pf") + 
                 won_by_pre_election_minister + 
                 PopulationDensityLog + acled_electoral_violence_dummy + year_character + 
                 other_petitions + distance_to_winning_vote_share ,  
               data = filter(petitionsByLosingCandidate, petition_and_litigation == 1, year >2011),
               family = binomial(link = "probit"))

summary(probit2)

probit3 <- glm(nullified_by_high_court_logical ~ appointing_party_high_court_judge * year_character + 
                 respondent_from_preelection_ruling_party +
                 won_by_pre_election_minister + 
                 PopulationDensityLog + acled_electoral_violence_dummy + year_character + 
                 other_petitions + distance_to_winning_vote_share ,  
               data = filter(petitionsByLosingCandidate, petition_and_litigation == 1, year >2011),
               family = binomial(link = "probit"))

summary(probit3)

texreg(list(probit1, probit2, probit3), 
       caption = "Probit models of High Court nullifications",
       file = "FiguresAndTables/probitModels.tex", 
       custom.model.names = paste("Model", 8:10), 
       size = "scriptsize", 
       label = "tab:probits", 
       include.deviance = FALSE,
       include.aic = FALSE,
       include.bic = FALSE,
       caption.above = TRUE,
       custom.coef.map = list("(Intercept)" = "Intercept",
                              "appointing_party_high_court_judgepf" = "PF Appointee",
                              
                              'I(winning_party == "pf")TRUE'= "Constituency won by PF",
                              'appointing_party_high_court_judgepf:I(winning_party == "pf")TRUE' = "PF Appointee * Constituency won by PF",
         
                              "year_character2021" = "2021 election",
                              "appointing_party_high_court_judgepf:year_character2021" = "PF Appointee * 2021 election",


                              "candidate_from_winning_party" = "Candidate from winning party",
                              "respondent_from_preelection_ruling_party" = "Constituency won by pre-election ruling party",
                              "won_by_pre_election_minister" = "Constituency won by pre-election minister",
                              "acled_electoral_violence_dummy" = "Electoral Violence",
                              "PopulationDensityLog" = "log(population)",
                              "other_petitions" =  "Other petitions in same constituency",
                              "distance_to_winning_vote_share" = "Distance to winning vote share"
                              ))



### Models of how claims in the 2011 election correlate with outcomes ####


formulas <- paste0("nullified_by_high_court_logical ~ ", c("petition_bribery_or_treating_claim_dummy",
                                                           "petition_abuse_state_resouces_claim_dummy",
                                                           "petition_ferrying_voters_claim_dummy",
                                                           "petition_violence_claim_dummy",
                                                           "petition_intimidation_claim_dummy",
                                                           "petition_malicious_statements_claim_dummy",
                                                           "petition_voting_or_count_irregularities_claim_dummy",
                                                           "petition_campaigning_outside_campaign_period_claim_dummy", 
                                                           "petition_other_claim_dummy"),  "+ distance_to_winning_vote_share")

claims_models <- map(.x = formulas, 
                     .f = function(x){
                       mod <- glm(as.formula(x), 
                                  data = filter(petitionsByLosingCandidate, petition == 1, year == 2011),
                                  family = binomial(link = "probit"))
                       
                     })
claims_models[[10]] <- glm(nullified_by_high_court_logical ~ petition_bribery_or_treating_claim_dummy +
                            petition_abuse_state_resouces_claim_dummy+
                            petition_ferrying_voters_claim_dummy+
                            petition_violence_claim_dummy +
                            petition_intimidation_claim_dummy+
                            petition_malicious_statements_claim_dummy +
                            petition_voting_or_count_irregularities_claim_dummy +
                            petition_campaigning_outside_campaign_period_claim_dummy+
                            petition_other_claim_dummy +
                            + distance_to_winning_vote_share, 
                          data = filter(petitionsByLosingCandidate, petition == 1, year == 2011),
                          family = binomial(link = "probit"))
claims_models[[11]] <- glm(nullified_by_high_court_logical ~ 
                             petition_abuse_state_resouces_claim_dummy+
                             petition_ferrying_voters_claim_dummy+
                             petition_violence_claim_dummy +
                             petition_intimidation_claim_dummy+
                             petition_malicious_statements_claim_dummy +
                             petition_voting_or_count_irregularities_claim_dummy +
                             petition_campaigning_outside_campaign_period_claim_dummy+
                             petition_other_claim_dummy +
                             + distance_to_winning_vote_share, 
                           data = filter(petitionsByLosingCandidate, petition == 1, year == 2011),
                           family = binomial(link = "probit"))

claims_models_map <- list( "petition_bribery_or_treating_claim_dummy" = "Alleged vote buying, bribery, or treating of voters", 
                           "petition_abuse_state_resouces_claim_dummy" =  "Alleged abuse of state resources", 
                           "petition_ferrying_voters_claim_dummy"  = "Alleged ferrying of voters", 
                           "petition_violence_claim_dummy" = "Alleged violence", 
                           "petition_intimidation_claim_dummy" = "Alleged intimidation of voters or other political parties",
                           "petition_malicious_statements_claim_dummy" = "Alleged malicious or false statements about opponents", 
                           "petition_voting_or_count_irregularities_claim_dummy" = "Alleged irregularities at polling station or during counting",
                           "petition_campaigning_outside_campaign_period_claim_dummy" = "Campaigning outside official campaign period",
                           "petition_other_claim_dummy" = "Other alleged violations of the Electoral Code", 
                           "candidate_from_winning_party" = "Candidate from winning party",
                           "distance_to_winning_vote_share" = "Distance to winning vote share", 
                           "(Intercept)" = "Intercept")

texreg(l = claims_models, 
          custom.coef.map = claims_models_map, 
          custom.model.names = paste0("(", 1:11,")"), 
          include.deviance = FALSE,
          include.aic = TRUE,
          include.bic = FALSE,
          caption.above = TRUE, 
       fontsize = "footnotesize",
       caption = "Probit models of the relationship between types of claims and High Court nullifications following the 2011 Elections",
       label = "tab:claims2011highcourt",
       file = "FiguresAndTables/claims2011highcourt.tex")


## Same but for the final outcome ###
formulas_final <- paste0("final_outcome_nullification_logical ~ ", c("petition_bribery_or_treating_claim_dummy",
                                                           "petition_abuse_state_resouces_claim_dummy",
                                                           "petition_ferrying_voters_claim_dummy",
                                                           "petition_violence_claim_dummy",
                                                           "petition_intimidation_claim_dummy",
                                                           "petition_malicious_statements_claim_dummy",
                                                           "petition_voting_or_count_irregularities_claim_dummy",
                                                           "petition_campaigning_outside_campaign_period_claim_dummy", 
                                                           "petition_other_claim_dummy"),  "+ distance_to_winning_vote_share")

claims_models_final <- map(.x = formulas_final, 
                     .f = function(x){
                       mod <- glm(as.formula(x), 
                                  data = filter(petitionsByLosingCandidate, petition == 1, year == 2011),
                                  family = binomial(link = "probit"))
                       
                     })
claims_models_final[[10]] <- glm(final_outcome_nullification_logical ~ petition_bribery_or_treating_claim_dummy +
                             petition_abuse_state_resouces_claim_dummy+
                             petition_ferrying_voters_claim_dummy+
                             petition_violence_claim_dummy +
                             petition_intimidation_claim_dummy+
                             petition_malicious_statements_claim_dummy +
                             petition_voting_or_count_irregularities_claim_dummy +
                             petition_campaigning_outside_campaign_period_claim_dummy+
                             petition_other_claim_dummy +
                             + distance_to_winning_vote_share, 
                           data = filter(petitionsByLosingCandidate, petition == 1, year == 2011),
                           family = binomial(link = "probit"))
claims_models_final[[11]] <- glm(final_outcome_nullification_logical ~ 
                             petition_abuse_state_resouces_claim_dummy+
                             petition_ferrying_voters_claim_dummy+
                             petition_violence_claim_dummy +
                             petition_intimidation_claim_dummy+
                             petition_malicious_statements_claim_dummy +
                             petition_voting_or_count_irregularities_claim_dummy +
                             petition_campaigning_outside_campaign_period_claim_dummy+
                             petition_other_claim_dummy +
                             + distance_to_winning_vote_share, 
                           data = filter(petitionsByLosingCandidate, petition == 1, year == 2011),
                           family = binomial(link = "probit"))

claims_models_map <- list( "petition_bribery_or_treating_claim_dummy" = "Alleged vote buying, bribery, or treating of voters", 
                           "petition_abuse_state_resouces_claim_dummy" =  "Alleged abuse of state resources", 
                           "petition_ferrying_voters_claim_dummy"  = "Alleged ferrying of voters", 
                           "petition_violence_claim_dummy" = "Alleged violence", 
                           "petition_intimidation_claim_dummy" = "Alleged intimidation of voters or other political parties",
                           "petition_malicious_statements_claim_dummy" = "Alleged malicious or false statements about opponents", 
                           "petition_voting_or_count_irregularities_claim_dummy" = "Alleged irregularities at polling station or during counting",
                           "petition_campaigning_outside_campaign_period_claim_dummy" = "Campaigning outside official campaign period",
                           "petition_other_claim_dummy" = "Other alleged violations of the Electoral Code", 
                           "candidate_from_winning_party" = "Candidate from winning party",
                           "distance_to_winning_vote_share" = "Distance to winning vote share", 
                           "(Intercept)" = "Intercept")

texreg(l = claims_models_final, 
       custom.coef.map = claims_models_map, 
       custom.model.names = paste0("(", 1:11,")"), 
       include.deviance = FALSE,
       include.aic = TRUE,
       include.bic = FALSE,
       caption.above = TRUE, 
       fontsize = "footnotesize",
       caption = "Probit models of the relationship between types of claims and nullifications (final outcome) following the 2011 Elections",
       label = "tab:claims2011final",
       file = "FiguresAndTables/claims2011final.tex")



#### Analysis of whether petitioners are more likely to re-contest in the next election ###
petitionsByLosingCandidate$participation_next
rerun1 <- glm(participation_next ~ petition, 
                  family = binomial(link = "probit"), 
                  data = petitionsByLosingCandidate)
rerun1b <- glm(participation_next ~ petition_and_litigation, 
              family = binomial(link = "probit"), 
              data = petitionsByLosingCandidate)
rerun2 <- glm(participation_next ~ petition + 
                distance_to_winning_vote_share, 
              family = binomial(link = "probit"), 
              data = petitionsByLosingCandidate)
rerun2b <- glm(participation_next ~ petition_and_litigation + 
                 distance_to_winning_vote_share, 
               family = binomial(link = "probit"), 
               data = petitionsByLosingCandidate)
rerun3 <- glm(participation_next ~ petition + 
                distance_to_winning_vote_share + 
                candidate_from_winning_party +
                respondent_from_preelection_ruling_party +
                won_by_pre_election_minister +
                acled_electoral_violence_dummy +
                PopulationDensityLog +
                year_character +
                other_petitions, 
              family = binomial(link = "probit"), 
              data = petitionsByLosingCandidate)

rerun3b <- glm(participation_next ~ petition_and_litigation + 
                distance_to_winning_vote_share + 
                candidate_from_winning_party +
                respondent_from_preelection_ruling_party +
                won_by_pre_election_minister +
                acled_electoral_violence_dummy +
                PopulationDensityLog +
                year_character +
                other_petitions, 
              family = binomial(link = "probit"), 
              data = petitionsByLosingCandidate)

rerun_models <- list(rerun1,rerun1b, rerun2, rerun2b, rerun3, rerun3b)
texreg(l = rerun_models , 
       override.se = lapply(rerun_models, 
                            function(x){
                              tmp <- coeftest(x, .vcov = vcovCL(x, filter(petitionsByLosingCandidate, year < 2021)$constituency))
                              return(tmp[,2])
                            }), 
       override.pvalues = lapply(rerun_models , 
                                 function(x){
                                   tmp <- coeftest(x, .vcov = vcovCL(x, filter(petitionsByLosingCandidate, year < 2021)$constituency))
                                   return(tmp[,4])
                                 }), 
       custom.coef.map = list(  "(Intercept)" = "Intercept",
                                "petition" = "Petition filed", 
                                "petition_and_litigation" = "Petition litigated",
                                "candidate_from_winning_party" = "Candidate from winning party",
                                "respondent_from_preelection_ruling_party" = "Constituency won by pre-election ruling party", 
                                "won_by_pre_election_minister"             = "Constituency won by pre-election minister",
                                "other_petitions" =  "Other petitions in same constituency",
                                "distance_to_winning_vote_share" = "Distance to winning vote share", 
                                "year_character2016" = "2016 Election", 
                                "acled_electoral_violence_dummy"       = "Electoral violence (ACLED)",
                                "PopulationDensityLog"                 = "Log population density"), 
       custom.model.names = paste0("(", 1:length(rerun_models), ")"),
       custom.note = "%stars Standard errors clustered on constituency in parentheses", 
       caption = "Probit models of relationship between electoral petitions and the likelihood of running in the next election among losing party candidates in the 2011 and 2016 elections", 
       label = "tab:rerun", 
       fontsize = "tiny",
       include.deviance = FALSE,
       caption.above = TRUE,
       file = "FiguresAndTables/rerun_models.tex")

### Robustness checks using matching ####
matching_data_all <- petitionsByLosingCandidate %>% 
  select(petition, petition_and_litigation, candidate_from_winning_party,
           respondent_from_preelection_ruling_party,
           won_by_pre_election_minister, 
         violence_2011_2016_2021,
           PopulationDensityLog,
           year_character, 
           other_petitions,
           distance_to_winning_vote_share) %>% 
  drop_na()
first_stage_matching_petition_all_cem <- matchit(candidate_from_winning_party ~ 
                                               respondent_from_preelection_ruling_party +
                                               won_by_pre_election_minister+ 
                                                 violence_2011_2016_2021+
                                               PopulationDensityLog+
                                               year_character + 
                                               other_petitions +
                                               distance_to_winning_vote_share, 
                                             method = "cem", 
                                             data = matching_data_all)

summary(first_stage_matching_petition_all_cem )

first_stage_matching_petition_all_nearest <- matchit(candidate_from_winning_party ~ 
                                                   respondent_from_preelection_ruling_party +
                                                   won_by_pre_election_minister+ 
                                                     violence_2011_2016_2021+
                                                   PopulationDensityLog+
                                                   year_character + 
                                                   other_petitions +
                                                   distance_to_winning_vote_share, 
                                                 method = "nearest", 
                                                 distance = "mahalanobis", 
                                                 data = matching_data_all)

cobalt_var_names <- data.frame(old = c("respondent_from_preelection_ruling_party", "won_by_pre_election_minister",
                                       "violence_2011_2016_2021", "PopulationDensityLog",
                                       "year_character_2011", "year_character_2016", "year_character_2021", 
                                       "other_petitions", "distance_to_winning_vote_share"), 
                               new = str_wrap(c(  "Constituency won by pre-election ruling party", "Constituency won by pre-election minister",
                                         "Electoral violence (ACLED or observers)", "Log population density", "2011 election", 
                                         "2016 election", "2021 election", "Other petitions in same constituency", "Distance to winning vote share"), 10))

balance_first_stage_cem <- love.plot(bal.tab(first_stage_matching_petition_all_cem), 
          var.names = cobalt_var_names, 
          stars = "std", 
          size = 4, 
          shapes = c(19, 17),
          colors = c("tomato2", "cornflowerblue"),
          themes = list(theme(text = element_text(size = 15))), 
          position = "bottom", 
          title = "Coarsened exact matching")

balance_first_stage_nearest <- love.plot(bal.tab(first_stage_matching_petition_all_nearest), 
          var.names = cobalt_var_names, 
          stars = "std", 
          size = 4, 
          shapes = c(19, 17),
          colors = c("tomato2", "cornflowerblue"),
          themes = list(theme(text = element_text(size = 15))),
          position = "bottom", 
          title = "Nearest neighbor")

balance_first_stage <- ggarrange(balance_first_stage_cem, balance_first_stage_nearest)
ggsave(filename = "FiguresAndTables/balance_firststage.pdf", height = 10, width = 10)

matched_data_all_cem <- match.data(first_stage_matching_petition_all_cem)

matched_probit_petition_cem <- glm(petition ~ candidate_from_winning_party +
                        respondent_from_preelection_ruling_party+
                        won_by_pre_election_minister+
                          violence_2011_2016_2021+
                        PopulationDensityLog+
                        year_character +
                        other_petitions +
                        distance_to_winning_vote_share,
                       data = matched_data_all_cem, 
                       family = binomial(link = "probit"), 
                       weights = matched_data_all_cem$weights)

matched_probit_litigation_cem <- glm(petition_and_litigation ~ candidate_from_winning_party +
                         respondent_from_preelection_ruling_party+
                         won_by_pre_election_minister+
                           violence_2011_2016_2021+
                         PopulationDensityLog+
                         year_character +
                         other_petitions +
                         distance_to_winning_vote_share,
                       data = matched_data_all_cem, 
                       family = binomial(link = "probit"), 
                       weights = matched_data_all_cem$weights)

matched_data_all_nearest <- match.data(first_stage_matching_petition_all_nearest)

matched_probit_petition_nearest <- glm(petition ~ candidate_from_winning_party +
                                     respondent_from_preelection_ruling_party +
                                     won_by_pre_election_minister +
                                       violence_2011_2016_2021 +
                                     PopulationDensityLog +
                                     year_character +
                                     other_petitions +
                                     distance_to_winning_vote_share,
                                   data = matched_data_all_nearest, 
                                   family = binomial(link = "probit"), 
                                   weights = matched_data_all_nearest$weights)

matched_probit_litigation_nearest <- glm(petition_and_litigation ~ candidate_from_winning_party +
                                         respondent_from_preelection_ruling_party +
                                         won_by_pre_election_minister +
                                           violence_2011_2016_2021 +
                                         PopulationDensityLog +
                                         year_character +
                                         other_petitions +
                                         distance_to_winning_vote_share,
                                       data = matched_data_all_nearest, 
                                       family = binomial(link = "probit"), 
                                       weights = matched_data_all_nearest$weights)

first_stage_matching_models <- list(matched_probit_petition_cem, 
                                    matched_probit_litigation_cem, 
                                    matched_probit_petition_nearest, 
                                    matched_probit_litigation_nearest)

texreg(l = first_stage_matching_models, 
          override.se = lapply(first_stage_matching_models, 
                               function(x){
                                 tmp <- coeftest(x, .vcov = vcovCL(x, petitionsByLosingCandidate$constituency))
                                 return(tmp[,2])
                               }), 
          override.pvalues = lapply(first_stage_matching_models , 
                                    function(x){
                                      tmp <- coeftest(x, .vcov = vcovCL(x, petitionsByLosingCandidate$constituency))
                                      return(tmp[,4])
                                    }), 
          custom.coef.map = list(  "(Intercept)" = "Intercept",
                                   "petition" = "Petition filed", 
                                   "petition_and_litigation" = "Petition litigated",
                                   "candidate_from_winning_party" = "Candidate from winning party",
                                   "respondent_from_preelection_ruling_party" = "Constituency won by pre-election ruling party", 
                                   "won_by_pre_election_minister"             = "Constituency won by pre-election minister",
                                   "other_petitions" =  "Other petitions in same constituency",
                                   "distance_to_winning_vote_share" = "Distance to winning vote share", 
                                   "year_character2016" = "2016 Election", 
                                   "violence_2011_2016_2021"       = "Electoral violence (ACLED or observers)",
                                   "PopulationDensityLog"                 = "Log population density"), 
          custom.model.names = c("Petition", "Litigation", "Petition", "Litigation"), 
          custom.note = "%stars Standard errors clustered on constituency in parentheses", 
          caption = "Probit models of decision by losing candidates to challenge their loss in court after pre-processing the data using matching", 
          label = "tab:matching_petition_models", 
          custom.gof.rows = list("Matching algorithm" = c("CEM", "CEM", "Nearest", "Nearest")),
          fontsize = "scriptsize",
          include.deviance = FALSE,
          caption.above = TRUE, 
          file = "FiguresAndTables/matching_petition_models.tex")

#### Matching for adjudication stage
names(petitionsByLosingCandidate)
petitionsByLosingCandidate$violence_2011_2016_2021
matching_data_litigated <- petitionsByLosingCandidate %>% 
  filter(petition_and_litigation == 1) %>% 
  select(petition_and_litigation,
         final_outcome_nullification, 
         nullified_by_high_court,
         candidate_from_winning_party,
         respondent_from_preelection_ruling_party,
         won_by_pre_election_minister, 
         violence_2011_2016_2021,
         PopulationDensityLog,
         year_character, 
         other_petitions,
         distance_to_winning_vote_share) %>% 
  drop_na()


second_stage_matching_cem <- matchit(candidate_from_winning_party ~ 
                                                   #respondent_from_preelection_ruling_party +
                                                    won_by_pre_election_minister+
                                       violence_2011_2016_2021+
                                                   # PopulationDensityLog+
                                                    year_character + 
                                                   # other_petitions +
                                                   distance_to_winning_vote_share, 
                                                 method = "cem", 
                                                 data = matching_data_litigated)

summary(second_stage_matching_cem )



second_stage_matching_nearest <- matchit(candidate_from_winning_party ~ 
                                           #respondent_from_preelection_ruling_party +
                                           won_by_pre_election_minister+
                                           violence_2011_2016_2021+
                                           # PopulationDensityLog+
                                           year_character + 
                                           # other_petitions +
                                           distance_to_winning_vote_share, 
                                         method = "nearest", 
                                         distance = "mahalanobis", 
                                         data = matching_data_litigated)

summary(second_stage_matching_nearest)



balance_second_stage_cem <- love.plot(bal.tab(second_stage_matching_cem), 
                                     var.names = cobalt_var_names,
                                     stars = "std",
                                     size = 4,
                                     shapes = c(19, 17),
                                     colors = c("tomato2", "cornflowerblue"),
                                     themes = list(theme(text = element_text(size = 15))),
                                     position = "bottom",
                                     title = "Coarsened exact matching")

balance_second_stage_nearest <- love.plot(bal.tab(second_stage_matching_nearest),
                                         var.names = cobalt_var_names,
                                         stars = "std",
                                         size = 4,
                                         shapes = c(19, 17),
                                         colors = c("tomato2", "cornflowerblue"),
                                         themes = list(theme(text = element_text(size = 15))),
                                         position = "bottom",
                                         title = "Nearest neighbor")

balance_second_stage <- ggarrange(balance_second_stage_cem, balance_second_stage_nearest)
ggsave(filename = "FiguresAndTables/balance_secondstage.pdf", height = 10, width = 10)

matched_data_litigated_cem <- match.data(second_stage_matching_cem)


matched_probit_nullification_cem_minimal <- glm(final_outcome_nullification ~ candidate_from_winning_party +
                                          distance_to_winning_vote_share,
                                        data = matched_data_litigated_cem, 
                                        family = binomial(link = "probit"), 
                                        weights = matched_data_litigated_cem$weights)
matched_probit_nullification_cem <- glm(final_outcome_nullification ~ candidate_from_winning_party +
                                     respondent_from_preelection_ruling_party+
                                     won_by_pre_election_minister+
                                     violence_2011_2016_2021+
                                     PopulationDensityLog+
                                     year_character +
                                     other_petitions +
                                     distance_to_winning_vote_share,
                                   data = matched_data_litigated_cem, 
                                   family = binomial(link = "probit"), 
                                   weights = matched_data_litigated_cem$weights)

matched_probit_high_court_nullification_cem_minimal <- glm(nullified_by_high_court ~ candidate_from_winning_party +
                                                     distance_to_winning_vote_share,
                                                   data = matched_data_litigated_cem, 
                                                   family = binomial(link = "probit"), 
                                                   weights = matched_data_litigated_cem$weights)
matched_probit_high_court_nullification_cem <- glm(nullified_by_high_court ~ candidate_from_winning_party +
                                          respondent_from_preelection_ruling_party+
                                          won_by_pre_election_minister+
                                          violence_2011_2016_2021+
                                          PopulationDensityLog+
                                          year_character +
                                          other_petitions +
                                          distance_to_winning_vote_share,
                                        data = matched_data_litigated_cem, 
                                        family = binomial(link = "probit"), 
                                        weights = matched_data_litigated_cem$weights)

summary(matched_probit_high_court_nullification_cem)

second_stage_matching_models <- list(matched_probit_high_court_nullification_cem_minimal, 
                                     matched_probit_high_court_nullification_cem, 
                                     matched_probit_nullification_cem_minimal, 
                                     matched_probit_nullification_cem_minimal)

texreg(l = second_stage_matching_models,
       custom.coef.map = list(  "(Intercept)" = "Intercept",
                                "candidate_from_winning_party" = "Candidate from winning party",
                                "distance_to_winning_vote_share" = "Distance to winning vote share", 
                                "respondent_from_preelection_ruling_party" = "Constituency won by pre-election ruling party", 
                                "won_by_pre_election_minister"             = "Constituency won by pre-election minister",
                                "other_petitions" =  "Other petitions in same constituency",
                                "year_character2016" = "2016 Election", 
                                "violence_2011_2016_2021"       = "Electoral violence (ACLED or observers)",
                                "PopulationDensityLog"                 = "Log population density"), 
       custom.model.names = c("Nullified (HC)", "Nullified (HC)", "Nullified (final)", "Nullified (final)"), 
       caption = "Probit models of nullification outcomes after preprocessing the data using coarsened exact matching decision", 
       label = "tab:matching_court_models", 
       custom.gof.rows = list("Matching algorithm" = c("CEM", "CEM", "CEM", "CEM")),
       fontsize = "tiny",
       include.deviance = FALSE,
       caption.above = TRUE, 
      file = "FiguresAndTables/matching_court_models.tex")



### Controlling for spatial lags ####

spatial_lag_1 <- selection(selection = formula(petition ~ candidate_from_winning_party + 
                                                 respondent_from_preelection_ruling_party + won_by_pre_election_minister + 
                                                 petition_spatial_lag + acled_electoral_violence_dummy + PopulationDensityLog + 
                                                 year_character + other_petitions + distance_to_winning_vote_share), 
                           outcome = formula(final_outcome_nullification_logical ~ candidate_from_winning_party + 
                                               respondent_from_preelection_ruling_party + PopulationDensityLog + 
                                               won_by_pre_election_minister + petition_spatial_lag + 
                                               acled_electoral_violence_dummy + year_character + other_petitions + 
                                               distance_to_winning_vote_share), data = petitionsByLosingCandidate, 
                           method = "ml")

spatial_lag_2 <- selection(selection = formula(petition ~ 
                                                 candidate_from_winning_party + 
                                                 respondent_from_preelection_ruling_party +
                                                 won_by_pre_election_minister+ 
                                                 petition_spatial_lag +
                                                 acled_electoral_violence_dummy+
                                                 PopulationDensityLog +
                                                 year_character + 
                                                 other_petitions +
                                                 distance_to_winning_vote_share), 
                           outcome = formula(nullified_by_high_court_logical~ 
                                               candidate_from_winning_party + 
                                               respondent_from_preelection_ruling_party +
                                               won_by_pre_election_minister+ 
                                               petition_spatial_lag +
                                               acled_electoral_violence_dummy+
                                               PopulationDensityLog +
                                               year_character + 
                                               other_petitions +
                                               distance_to_winning_vote_share),  
                           data = petitionsByLosingCandidate, 
                           method = "ml")


makeSelectionModelTable(modelList = list( spatial_lag_2, spatial_lag_1), 
                        coefMapping = list(  "S: (Intercept)" = "S: Intercept",
                                             "S: candidate_from_winning_party" = "S: Candidate from winning party",
                                             "S: petition_spatial_lag" = "S: Spatial lag of petitions",
                                             "S: high_court_loser_from_winning_party"  = "S: High Court loser from winning party", 
                                             "S: respondent_from_preelection_ruling_party" = "S: Constituency won by pre-election ruling party", 
                                             "S: won_by_pre_election_minister"             = "S: Constituency won by pre-election minister",
                                             "S: other_petitions" =  "S: Other petitions in same constituency",
                                             "S: distance_to_winning_vote_share" = "S: Distance to winning vote share", 
                                             "S: year_character2016" = "S: 2016 Election", 
                                             "S: year_character2021" = "S: 2021 Election", 
                                             "S: appointing_party_high_court_judgepf" = "S: High Court judge appointed by PF", 
                                             "S: high_court_outcomePetition dismissed" = "S: Petition Dismissed in High Court", 
                                             "S: acled_electoral_violence_dummy"       = "S: Electoral violence (ACLED)",
                                             "S: violence_2016_2021"       = "S: Electoral violence (Election monitors)",
                                             "S: PopulationDensityLog"                 = "S: Log population density",
                                             
                                             "O: (Intercept)" = "O: Intercept", 
                                             "O: candidate_from_winning_party" = "O: Candidate from winning party",
                                             "O: petition_spatial_lag" = "O: Spatial lag of petitions",
                                             "O: high_court_loser_from_winning_party"  = "O: High Court loser from winning party", 
                                             "O: respondent_from_preelection_ruling_party" = "O: Constituency won by pre-election ruling party", 
                                             "O: won_by_pre_election_minister"             = "O: Constituency won by pre-election minister",
                                             "O: other_petitions" =  "O: Other petitions in same constituency",
                                             "O: distance_to_winning_vote_share" = "O: Distance to winning vote share", 
                                             "O: year_character2016" = "O: 2016 Election", 
                                             "O: year_character2021" = "O: 2021 Election", 
                                             "O: appointing_party_high_court_judgepf" = "O: High Court judge appointed by PF", 
                                             "O: high_court_outcomePetition dismissed" = "O: Petition Dismissed in High Court", 
                                             "O: acled_electoral_violence_dummy"       = "O: Electoral violence (ACLED)",
                                             "O: violence_2016_2021"       = "O: Electoral violence (Election monitors)",
                                             
                                             
                                             
                                             "O: PopulationDensityLog"                 = "O: Log population density",
                                             
                                             "rho" = "O: rho"), 
                        selectionOutcome = c("", "Petition", "Petition"), 
                        outcomeOutcome = c("", "Nullified", "Nullified by High Court"), 
                        filename = "FiguresAndTables/SpatialLag.tex", 
                        caption = "Sample-selection models with spatial lags of petitions", 
                        size = "tiny",
                        label = "tab: tab:spatial_lag")






